package com.beilang.base.util;


import com.beilang.hotline.telreply.model.TelreplyModel;
import com.beilang.hotline.telrecord.model.TelrecordModel;
import com.beilang.hotline.email.model.EmailServerModel;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeBodyPart;
import javax.activation.DataHandler;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Iterator;
import java.util.Date;
import java.io.UnsupportedEncodingException;


public class SendEmailUtil {


    public static final String MAIL_ENCODING = "gbk";

    public static boolean sendMail(String from, String displayName, ArrayList toList, String mailhost,
                            String mailuserName, String mailpassword, String mailSubject,
                            TelreplyModel telreplyModel, TelrecordModel telrecordModel,
                            EmailServerModel emailServerModel) {
        Properties props = System.getProperties();
        props.put("mail.smtp.host", mailhost);
        props.put("mail.smtp.auth", "true"); //这样才能通过验证

        MyAuthenticator myauth = new MyAuthenticator(mailuserName, mailpassword);
        Session session = Session.getDefaultInstance(props, myauth);

        // Define message
        MimeMessage message = new MimeMessage(session);

        // Set the from address
        try {
            message.setFrom(new InternetAddress(from,displayName));

            // Set the to address
//            toList
            Address[] toAddressList = new Address[toList.size()];


            int i = 0;
            if (toList != null && !toList.isEmpty()) {
                Iterator itor = toList.iterator();
                while (itor.hasNext()) {
                    toAddressList[i] = new InternetAddress((String) itor.next());
                    i++;
                }
            }

            message.addRecipients(Message.RecipientType.TO, toAddressList);
            sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
            if (mailSubject == null) {
                mailSubject = "";
            }

            mailSubject = "=?" + MAIL_ENCODING + "?B?" + enc.encode(mailSubject.getBytes(MAIL_ENCODING)) + "?=";

            // Set the subject
            message.setSubject(mailSubject);

            MimeMultipart mp = new MimeMultipart();

            mp.setSubType("related");
            BodyPart bodyPart = new MimeBodyPart();// 正文
            bodyPart.setDataHandler(new DataHandler(getHTMLContent(telreplyModel,telrecordModel,emailServerModel),
                "text/html;charset=GBK"));// 网页格式
            mp.addBodyPart(bodyPart);
            message.setContent(mp);
            message.setSentDate(new Date());

            message.saveChanges();

            Transport.send(message);
            return true;
        } catch (MessagingException e) {
            System.out.printf("邮件发送失败");
        } catch (UnsupportedEncodingException e) {
            System.out.printf("邮件发送失败");
        }
        return false;
    }

    private static String  getHTMLContent( TelreplyModel telreplyModel, TelrecordModel telrecordModel,EmailServerModel emailServerModel)
    {
         StringBuffer s = new   StringBuffer();
        s.append("<html>");
        s.append("<head>");
        s.append("<style>");
        s.append(" table{border-collapse:collapse;border-spacing:0;border-left:1px solid #888;border-top:1px solid #888;}");
        s.append(" th,td{border-right:1px solid #888;border-bottom:1px solid #888;padding:5px 15px;}");
        s.append(" th{font-weight:bold;}");
        s.append("</style>");
        s.append("</head>");
        s.append("<body>");
        s.append("<P>");
        s.append(" <b>创建人</b>："+StringUtil.deNull(telrecordModel.getCreatorNameDesc())+"<br>");
        s.append(" <b>所属项目</b>："+StringUtil.deNull(telrecordModel.getSysNameDesc())+"<br>");
        s.append(" <b>事物名称</b>："+StringUtil.deNull(telrecordModel.getTitleRemarks())+"<br>");
        s.append(" <b>当前状态</b>："+StringUtil.deNull(telrecordModel.getRecordStatusDesc())+"<br>");
        s.append(" <b>当前处理人</b>："+StringUtil.deNull(telrecordModel.getCurOwnerNameDesc())+"<br>");
        s.append(" <hr size=\"1\" noshade=\"noshade\" style=\"border:1px #cccccc dotted;\">");
        s.append(" <b>打开事务页面:");
        s.append(" <a href=\""+StringUtil.deNull(emailServerModel.getEmailUrl())+"/\">");
        s.append(StringUtil.deNull(emailServerModel.getEmailUrl())+"</a></b>");
        s.append(" <hr size=\"1\" noshade=\"noshade\" style=\"border:1px #cccccc;\">");
        s.append(" <b>事务信息</b>:");
        s.append("</P>");
        s.append("<table>");
        s.append(" <tr>");
        s.append("    <td><b>来电时间</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getRecordTimeStr())+"</td>");
        s.append(" </tr>");
         s.append(" <tr>");
        s.append("    <td><b>企业</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getCompanyNameDesc())+"</td>");
        s.append(" </tr>");
         s.append(" <tr>");
        s.append("    <td><b>ID号</b>:</td>");
        s.append("    <td>"+telrecordModel.getRecordId()+"</td>");
        s.append(" </tr>");
         s.append(" <tr>");
        s.append("    <td><b>姓名</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getPname())+"</td>");
        s.append(" </tr>");
         s.append(" <tr>");
        s.append("    <td><b>性别</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getSexDesc())+"</td>");
        s.append(" </tr>");
         s.append(" <tr>");
        s.append("    <td><b>来电电话</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getRecordNumber())+"</td>");
        s.append(" </tr>");
         s.append(" <tr>");
        s.append("    <td><b>系统</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getSysNameDesc())+"</td>");
        s.append(" </tr>");
         s.append(" <tr>");
        s.append("    <td><b>模块</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getModuleNameDesc())+"</td>");
        s.append(" </tr>");
         s.append(" <tr>");
        s.append("    <td><b>类别</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getIssueNameDesc())+"</td>");
        s.append(" </tr>");
          s.append(" <tr>");
        s.append("    <td><b>标题</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getTitleRemarks())+"</td>");
        s.append(" </tr>");
          s.append(" <tr>");
        s.append("    <td><b>问题描述及回复</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getRecordRemarks())+"</td>");
        s.append(" </tr>");
          s.append(" <tr>");
        s.append("    <td><b>问题原因</b>:</td>");
        s.append("    <td>"+StringUtil.deNull(telrecordModel.getRecordReason())+"</td>");
        if (telreplyModel!=null&&telreplyModel.getReplyTime()!=null)
        {
            s.append(" </tr>");
            s.append(" <tr>");
            s.append("    <td colspan=\"2\"><b>处理</b></td>");
            s.append(" </tr>");
            s.append(" <tr>");
            s.append("    <td><b>处理时间</b>:</td>");
            s.append("    <td>"+StringUtil.deNull(telreplyModel.getReplyTimeStr())+"</td>");
            s.append(" </tr>");
            s.append(" <tr>");
            s.append("    <td><b>处理内容</b>:</td>");
            s.append("    <td>"+StringUtil.deNull(telreplyModel.getReplyContent())+"</td>");
            s.append(" </tr>");
            s.append(" <tr>");
            s.append("    <td><b>处理原因</b>:</td>");
            s.append("    <td>"+StringUtil.deNull(telreplyModel.getReason())+"</td>");
            s.append(" </tr>");
        }
        s.append("</table>");
        return s.toString();
    }
}
